package com.javamall.config;

import com.javamall.interceptor.SysInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * web项目配置类
 * @author: javaLiuwb
 * @date: 2024-02-14 10:54
 * @description:
 */
@Configuration
public class WebAppConfigure implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/image/findSwiper/**")
                .addResourceLocations("file:D:\\codeWorkspace\\javaWorkspace\\data\\malldata\\swiperImgs\\");
        registry.addResourceHandler("/image/bigType/**")
                .addResourceLocations("file:D:\\codeWorkspace\\javaWorkspace\\data\\malldata\\bigTypeImgs\\");
        registry.addResourceHandler("/image/product/**")
                .addResourceLocations("file:D:\\codeWorkspace\\javaWorkspace\\data\\malldata\\productImgs\\");
        registry.addResourceHandler("/image/productSwiperImgs/**")
                .addResourceLocations("file:D:\\codeWorkspace\\javaWorkspace\\data\\malldata\\productSwiperImgs\\");
        registry.addResourceHandler("/image/productIntroImgs/**")
                .addResourceLocations("file:D:\\codeWorkspace\\javaWorkspace\\data\\malldata\\productIntroImgs\\");
        registry.addResourceHandler("/image/productParaImgs/**")
                .addResourceLocations("file:D:\\codeWorkspace\\javaWorkspace\\data\\malldata\\productParaImgs\\");
    }

    @Bean
    public SysInterceptor sysInterceptor(){
        return new SysInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        String[] patterns=new String[]{"/adminLogin","/product/**","/bigType/**","/user/wxlogin","/weixinpay/**"};
        registry.addInterceptor(sysInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(patterns);
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE","OPTIONS")
                .maxAge(3600);
    }
}
